import time

import pytest
from Common.log_opertion import logs
from Common.yaml_opertion import YamlOpertion
from Common.ding_robot_pull import DingRobotPull
from Common.jenkins_opertion import JenkinsOpertion
from base.generate_id import t_id
dr=DingRobotPull()
jo=JenkinsOpertion()

@pytest.fixture(scope='session',autouse=True)
def fixfunc01():
    logs.info('---------------接口请求开始---------------')
    YamlOpertion().clean_yaml()
    logs.info('extract.yml文件已清空')
    yield
    logs.info('---------------接口请求结束---------------')

@pytest.fixture(scope='function',autouse=True)
def test_case_index():
    logs.info(f'---------------第{next(t_id)}个用例测试---------------')

def pytest_terminal_summary(terminalreporter,exitstatus,config):
    """
    terminalreporter:测试结果的统计数据的容器对象
    exitstatus:pytest退出状态码
    - `0`：全部测试通过
    - `1`：存在失败用例
    - `2`：测试收集失败
    - `5`：未收集到用例
    config:运行时配置
    """
    tr=terminalreporter
    case_total=tr._numcollected
    stats=tr.stats
    passed=len(stats.get('passed',[]))
    failed=len(stats.get('failed',[]))
    skipped=len(stats.get('skipped',[]))
    error=len(stats.get('error',[]))

    # 通过率
    rate=passed/case_total*100

    content=f"""=========测试摘要=========
用例总数:{case_total}
通过: {passed} | 失败: {failed} | 跳过: {skipped} | 异常:{error}
通过率: {rate}%
    """
    # 点击查看allure报告:
    # {jo.report_success_or_fail()}
    dr.send_ding_msg(content=content)